package com.wechat.ser;

import java.io.Serializable;

import com.swak.utils.JaxbMapper;
import com.swak.utils.StringUtils;

import lombok.Getter;
import lombok.Setter;
import lombok.experimental.Accessors;

/**
 * 服务
 * 
 * @author 618lf
 */
@Getter
@Setter
@Accessors(chain = true)
public class Ser implements Serializable {

	private static final long serialVersionUID = 1L;
	
	@SuppressWarnings("unchecked")
	public <T extends Ser> T as() {
		return (T) this;
	}
	
	/**
	 * 转为 XML 存储
	 * 
	 * @return
	 */
	@Override
	public String toString() {
		return JaxbMapper.toXml(this);
	}

	public String toXml() {
		return JaxbMapper.toXml(this);
	}

	public static <T> T fromXml(String xml, Class<T> clazz) {
		if (StringUtils.isBlank(xml) || clazz == null) {
			return null;
		}
		return JaxbMapper.fromXml(xml, clazz);
	}
}
